package com.mzj.saas.aspectj;


import cn.hutool.core.convert.Convert;
import com.haixiaoke.saas.repository.exception.BizException;
import com.mzj.saas.annotation.CheckCertification;
import com.mzj.saas.commons.StatusCode;
import com.mzj.saas.commons.util.ServletUtil;
import com.mzj.saas.commons.vo.redis.TokenRedisVo;
import com.mzj.saas.redis.RedisService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 实名认证校验切面
 */
@Aspect
@Component
public class CheckCertificationAspect {

    @Autowired
    private RedisService redisService;

    @Before("@annotation(certification)")
    public void doBefore(JoinPoint point, CheckCertification certification) {
        String accessToken = ServletUtil.getHeader("accesstoken");
        TokenRedisVo tokenRedisVo = redisService.findTokenVo(accessToken);
        if (tokenRedisVo == null) {
            Integer errorCode = Convert.toInt(StatusCode.ERROR_CODE_40001.getErrorCode());
            String errorMsg = StatusCode.ERROR_CODE_40001.getErrorMsg();
            throw new BizException(errorCode, errorMsg);
        }
        Integer certificationStatus = tokenRedisVo.getCertificationStatus();
        if (certificationStatus == 0) {
            Integer errorCode = Convert.toInt(StatusCode.ERROR_CODE_50001.getErrorCode());
            String errorMsg = StatusCode.ERROR_CODE_50001.getErrorMsg();
            throw new BizException(errorCode, errorMsg);
        }
    }

}
